This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathPlaylistToast.swift
344 lines (290 loc) · 11 KB
/
PlaylistToast.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import Foundation
import UIKit
import BraveUI
import Shared
import SnapKit
import Data
import DesignSystem
private class ToastShadowView: UIView {
private var shadowLayer: CAShapeLayer?
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath(
roundedRect: bounds,
cornerRadius: ButtonToastUX.toastButtonBorderRadius
).cgPath
if let shadowLayer = shadowLayer {
shadowLayer.path = path
shadowLayer.shadowPath = path
} else {
shadowLayer = CAShapeLayer().then {
$0.path = path
$0.fillColor = UIColor.clear.cgColor
$0.shadowColor = UIColor.black.cgColor
$0.shadowPath = path
$0.shadowOffset = .zero
$0.shadowOpacity = 0.5
$0.shadowRadius = ButtonToastUX.toastButtonBorderRadius
}
shadowLayer?.do {
layer.insertSublayer($0, at: 0)
}
}
}
}
private class HighlightableButton: UIButton {
private var shadowLayer: CAShapeLayer?
var shadowLayerZOrder: Int {
guard let shadowLayer = shadowLayer else { return -1 }
return self.layer.sublayers?.firstIndex(of: shadowLayer) ?? -1
}
var isShadowHidden: Bool = false {
didSet {
shadowLayer?.isHidden = isShadowHidden
}
}
override var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? UIColor.white.withAlphaComponent(0.2) : .clear
}
}
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath(
roundedRect: bounds,
cornerRadius: ButtonToastUX.toastButtonBorderRadius
).cgPath
if let shadowLayer = shadowLayer {
shadowLayer.path = path
shadowLayer.shadowPath = path
} else {
shadowLayer = CAShapeLayer().then {
$0.path = path
$0.fillColor = UIColor.clear.cgColor
$0.shadowColor = UIColor.black.cgColor
$0.shadowPath = path
$0.shadowOffset = .zero
$0.shadowOpacity = 0.5
$0.shadowRadius = ButtonToastUX.toastButtonBorderRadius
}
shadowLayer?.do {
layer.insertSublayer($0, at: 0)
}
}
}
}
class PlaylistToast: Toast {
private struct DesignUX {
static let maxToastWidth: CGFloat = BraveUX.baseDimensionValue
}
private let toastShadowView = ToastShadowView()
private lazy var gradientView = BraveGradientView.gradient02
private let button = HighlightableButton()
private var panState: CGPoint = .zero
private let state: PlaylistItemAddedState
var item: PlaylistInfo?
init(item: PlaylistInfo?, state: PlaylistItemAddedState, completion: ((_ buttonPressed: Bool) -> Void)?) {
self.item = item
self.state = state
super.init(frame: .zero)
self.completionHandler = completion
toastView.backgroundColor = .clear
clipsToBounds = false
addSubview(createView(item, state))
toastView.snp.makeConstraints {
$0.leading.trailing.height.equalTo(self)
self.animationConstraint = $0.top.equalTo(self).offset(ButtonToastUX.toastHeight).constraint
}
self.snp.makeConstraints {
$0.height.equalTo(ButtonToastUX.toastHeight)
}
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(onSwipeToDismiss(_:)))
toastView.addGestureRecognizer(panGesture)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func createView(_ item: PlaylistInfo?, _ state: PlaylistItemAddedState) -> UIView {
if state == .newItem || state == .existingItem {
let horizontalStackView = UIStackView().then {
$0.alignment = .center
$0.spacing = ButtonToastUX.toastPadding
}
let labelStackView = UIStackView().then {
$0.axis = .vertical
$0.alignment = .leading
}
let label = UILabel().then {
$0.textAlignment = .left
$0.textColor = .white
$0.font = ButtonToastUX.toastLabelFont
$0.lineBreakMode = .byWordWrapping
$0.numberOfLines = 0
if state == .newItem {
$0.text = Strings.PlayList.toastAddedToPlaylistTitle
} else {
$0.text = Strings.PlayList.toastExitingItemPlaylistTitle
}
}
self.button.do {
$0.layer.cornerRadius = ButtonToastUX.toastButtonBorderRadius
$0.layer.borderWidth = ButtonToastUX.toastButtonBorderWidth
$0.layer.borderColor = UIColor.white.cgColor
$0.imageView?.tintColor = .white
$0.setTitle(Strings.PlayList.toastAddToPlaylistOpenButton, for: [])
$0.setTitleColor(.white, for: .highlighted)
$0.titleLabel?.font = SimpleToastUX.toastFont
$0.titleLabel?.numberOfLines = 1
$0.titleLabel?.lineBreakMode = .byClipping
$0.titleLabel?.adjustsFontSizeToFitWidth = true
$0.titleLabel?.minimumScaleFactor = 0.1
$0.isShadowHidden = true
$0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(buttonPressed)))
}
self.button.snp.makeConstraints {
if let titleLabel = self.button.titleLabel {
$0.width.equalTo(titleLabel.intrinsicContentSize.width + 2 * ButtonToastUX.toastButtonPadding)
}
}
labelStackView.addArrangedSubview(label)
horizontalStackView.addArrangedSubview(labelStackView)
horizontalStackView.addArrangedSubview(button)
toastView.addSubview(toastShadowView)
toastView.addSubview(horizontalStackView)
toastShadowView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
horizontalStackView.snp.makeConstraints {
$0.centerX.equalTo(toastView)
$0.centerY.equalTo(toastView)
$0.width.equalTo(toastView.snp.width).offset(-2 * ButtonToastUX.toastPadding)
}
updateGradientView()
return toastView
}
let horizontalStackView = UIStackView().then {
$0.alignment = .center
$0.spacing = ButtonToastUX.toastPadding
}
self.button.do {
$0.layer.cornerRadius = ButtonToastUX.toastButtonBorderRadius
$0.backgroundColor = .clear
$0.setTitleColor(.white, for: .highlighted)
$0.imageView?.tintColor = .white
$0.tintColor = .white
$0.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .medium)
$0.titleLabel?.numberOfLines = 1
$0.titleLabel?.lineBreakMode = .byClipping
$0.titleLabel?.adjustsFontSizeToFitWidth = true
$0.titleLabel?.minimumScaleFactor = 0.1
$0.contentHorizontalAlignment = .left
$0.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 20.0)
$0.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: -10.0)
$0.isShadowHidden = false
$0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(buttonPressed)))
}
horizontalStackView.addArrangedSubview(button)
toastView.addSubview(horizontalStackView)
horizontalStackView.snp.makeConstraints {
$0.centerX.equalTo(toastView)
$0.centerY.equalTo(toastView)
$0.width.equalTo(toastView.snp.width).offset(-2 * ButtonToastUX.toastPadding)
}
if state == .none {
button.setImage(UIImage(named: "quick_action_new_tab", in: .module, compatibleWith: nil)!.template, for: [])
button.setTitle(Strings.PlayList.toastAddToPlaylistTitle, for: [])
} else {
assertionFailure("Should Never get here. Others case are handled at the start of this function.")
}
updateGradientView()
return toastView
}
@objc func buttonPressed(_ gestureRecognizer: UIGestureRecognizer) {
completionHandler?(true)
dismiss(true)
}
@objc override func handleTap(_ gestureRecognizer: UIGestureRecognizer) {
dismiss(false)
}
override func showToast(viewController: UIViewController? = nil, delay: DispatchTimeInterval, duration: DispatchTimeInterval?, makeConstraints: @escaping (SnapKit.ConstraintMaker) -> Swift.Void) {
super.showToast(viewController: viewController, delay: delay, duration: duration) {
guard let viewController = viewController as? BrowserViewController else {
assertionFailure("Playlist Toast should only be presented on BrowserViewController")
return
}
$0.centerX.equalTo(viewController.view.snp.centerX)
$0.bottom.equalTo(viewController.webViewContainer.safeArea.bottom)
$0.leading.equalTo(viewController.view.safeArea.leading).priority(.high)
$0.trailing.equalTo(viewController.view.safeArea.trailing).priority(.high)
$0.width.lessThanOrEqualTo(DesignUX.maxToastWidth)
}
}
override func dismiss(_ buttonPressed: Bool) {
self.dismiss(buttonPressed, animated: true)
}
func dismiss(_ buttonPressed: Bool, animated: Bool) {
if displayState == .pendingDismiss || displayState == .dismissed {
return
}
displayState = .pendingDismiss
superview?.removeGestureRecognizer(gestureRecognizer)
layer.removeAllAnimations()
let duration = animated ? SimpleToastUX.toastAnimationDuration : 0.1
UIView.animate(
withDuration: duration,
animations: {
self.animationConstraint?.update(offset: SimpleToastUX.toastHeight)
self.layoutIfNeeded()
}
) { finished in
self.displayState = .dismissed
self.removeFromSuperview()
if !buttonPressed {
self.completionHandler?(false)
}
}
}
private var shadowLayerZOrder: Int {
if state == .newItem || state == .existingItem {
// In this state, the shadow is on the toastView
let index = toastView.subviews.firstIndex(of: toastShadowView) ?? -1
return index + 1
}
// In this state, the shadow is on the button itself
return button.shadowLayerZOrder + 1
}
private func updateGradientView() {
gradientView.removeFromSuperview()
if state == .newItem || state == .existingItem {
toastView.insertSubview(gradientView, at: shadowLayerZOrder)
} else {
button.insertSubview(gradientView, at: shadowLayerZOrder)
}
gradientView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
}
@objc
private func onSwipeToDismiss(_ recognizer: UIPanGestureRecognizer) {
// Distance travelled after decelerating to zero velocity at a constant rate
func project(initialVelocity: CGFloat, decelerationRate: CGFloat) -> CGFloat {
return (initialVelocity / 1000.0) * decelerationRate / (1.0 - decelerationRate)
}
if recognizer.state == .began {
panState = toastView.center
} else if recognizer.state == .ended {
let velocity = recognizer.velocity(in: toastView)
if abs(velocity.y) > abs(velocity.x) {
let y = min(panState.y, panState.y + recognizer.translation(in: toastView).y)
let projected = project(initialVelocity: velocity.y, decelerationRate: UIScrollView.DecelerationRate.normal.rawValue)
if y + projected > toastView.frame.maxY {
dismiss(false)
}
}
}
}
}